package com.easidea.tim.flex.remote.system{
	import com.easidea.tim.flex.Env;
	import com.easidea.tim.flex.bean.system.Geography;
	import com.easidea.tim.flex.event.CustomerEvent;
	import com.easidea.tim.flex.remote.BaseDao;
	import com.easidea.tim.flex.util.AppAlert;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	import net.digitalprimates.persistence.hibernate.rpc.HibernateRemoteObject;
	
	public class GeographyDao extends BaseDao {		private var _geographyService:HibernateRemoteObject = new HibernateRemoteObject();		private static var _instance:GeographyDao;		[Bindable] private var currentGeoId : int;				/**		 * Singleton 		 */		public static function getInstance() : GeographyDao{			if(_instance == null){				_instance = new GeographyDao();			}			return _instance;		}				/**		 * 构造函数		 */		public function GeographyDao() : void{			_geographyService.channelSet = this.buildChannelSet();			_geographyService.destination="geographyService";				_geographyService.concurrency = "last";			_geographyService.makeObjectsBindable = true;			_geographyService.showBusyCursor = true;		}				/**** ------------ 保存数据 ----------------------------------------------------- ****/		/**		 * 进行geography数据保存处理的业务方法，需要调用对应spring 的service的save方法		 * @param geography 		 */		public function saveGeography(geo : Geography) : void{			_geographyService.addEventListener(ResultEvent.RESULT, processSaveGeo);			_geographyService.addEventListener(FaultEvent.FAULT, _Fault);			_geographyService.saveGeo(geo); // 调用spring service对应方法		}				private function processSaveGeo(event: ResultEvent) : void{			_geographyService.removeEventListener(ResultEvent.RESULT, processSaveGeo);			var message : String;			if(event.result as String == "-2"){				// 保存二级数据失败，父对象找不到				message = "保存失败，未找到指定数据！";			}else if(event.result as String == "0"){				// 保存二级数据失败，父对象找不到				message = "保存失败，空数据！";			}else if(event.result as String == "2"){				message = "数据修改成功！";			}else {				message = "保存成功！";			}			AppAlert.show(message,"提示");			Env.dispatchEvent(new CustomerEvent("saveGeographyEvent",event.result));			  									 		}		/**** ----------------------结束---------------华丽的分割线--------------------------****/				/****----------- 查询根级数据 -----------------****/		/**		 * 略		 */		public function queryAllParent() : void {			_geographyService.addEventListener(ResultEvent.RESULT, processQueryAllParent);			_geographyService.addEventListener(FaultEvent.FAULT, _Fault);			_geographyService.listAreas(null); // 调用spring service对应方法		}				private function processQueryAllParent(event:ResultEvent) : void {			_geographyService.removeEventListener(ResultEvent.RESULT, processQueryAllParent);			if(event != null && event.result != null) {				var resultArray : ArrayCollection = event.result as ArrayCollection;				//trace(resultArray);				Env.dispatchEvent(new CustomerEvent("listAreasEvent",resultArray));			}		}		/**** ----------------------结束---------------华丽的分割线--------------------------****/				/**		 * 删除数据		 */		public function deleteGeo (id : int, modifier: String) : void {			if(id != -1 && id !=0){				_geographyService.addEventListener(ResultEvent.RESULT, processDelGeo);				_geographyService.addEventListener(FaultEvent.FAULT, _Fault);								var delGeo : Geography = new Geography();				delGeo.id = id;				currentGeoId = id;				delGeo.deleted = 1;				delGeo.modifier = modifier;								_geographyService.delGeo(delGeo);			}		}				private function processDelGeo(event: ResultEvent) : void {			_geographyService.removeEventListener(ResultEvent.RESULT, processSaveGeo);			switch (event.result as int){				case 0: // success					AppAlert.show("删除成功！","提示");					Env.dispatchEvent(new CustomerEvent("delGeographyEvent",true,currentGeoId));					break;				case -1: // null parameter					AppAlert.show("删除失败，系统异常！","提示");					Env.dispatchEvent(new CustomerEvent("delGeographyEvent",false));					break;				case -2: // 找不到ID					AppAlert.show("删除失败，指定数据不存在！","提示");					Env.dispatchEvent(new CustomerEvent("delGeographyEvent",false));					break;				case -3: // 有子对象					AppAlert.show("删除失败，指定数据有次级地区数据，不能删除！","提示");					Env.dispatchEvent(new CustomerEvent("delGeographyEvent",false));					break;			}		}	}
}